package com.smedi.ismedi.progress.core.port.adapter.restful;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.cache.starter.util.IdempotenceLock;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.progress.api.dto.config.Tasknotice;
import com.smedi.ismedi.progress.api.dto.request.*;
import com.smedi.ismedi.progress.api.dto.response.*;
import com.smedi.ismedi.progress.api.feign.RemoteProgressFeignApi;
import com.smedi.ismedi.progress.core.application.command.*;
import com.smedi.ismedi.progress.core.application.convert.AppConverter;
import com.smedi.ismedi.progress.core.application.query.*;
import com.smedi.ismedi.progress.core.application.service.*;
import com.smedi.ismedi.progress.core.domain.aggregatemodel.*;
import com.smedi.ismedi.progress.core.domain.dto.ProjectBriefReportVo;
import com.smedi.ismedi.progress.core.domain.repository.AttachmentRepository;
import com.smedi.ismedi.progress.core.domain.service.ProjectVersionDomainServer;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.persistence.model.AttachmentEntity;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 特种设备Controller
 *
 * @author xienannan
 * @date 2021/08/28
 */
@AllArgsConstructor
@RestController
public class ProgressController implements RemoteProgressFeignApi {
    private final AdapterConverter adapterConverter;
    private final ProgressQueryService progressQueryService;
    private final MilestoneQueryService milestoneQueryService;
    private final KeyTaskQueryService keyTaskQueryService;
    private final VersionManageQueryService versionManageQueryService;
    private final ProgressApplicationService progressApplicationService;
    private final ProgressViewQueryService progressViewQueryService;
    private final ProgressReportQueryService progressReportQueryService;
    private final ProgressReportApplicationService progressReportApplicationService;
    private final ConstructionLogApplicationService constructionLogApplicationService;
    private final ProjectVersionDomainServer progressVersionDomainServer;
    private final ProjectBriefReportQueryService projectBriefReportQueryService;
    private final ProjectBriefReportApplicationService projectBriefReportApplicationService;
    private final ConstructionLogQueryService constructionLogQueryService;
    private final ConstructionLogService constructionLogService;
    private final EngineeringrReportService engineeringrReportService;
    private final EngineeringReportApplicationService engineeringReportApplicationService;
    private final AppConverter appConverter;

    @Override
    public ResponseData<PaginationData<ProgressKanBanListResponse>> getProgressKanBanPage(ProgressKanBanRequest progressKanBanRequest) {
        ProgressKanBanQuery progressKanBanQuery = adapterConverter.progressKanBanRequestToProgressKanBanRequestQuery(progressKanBanRequest);
        Page<Progress> progressPage = progressQueryService.getProgressKanBanPage(progressKanBanQuery);
        PaginationData<ProgressKanBanListResponse> progressKanBanResponsePaginationData = adapterConverter.progressPageToPaginationKanBanData(progressPage);

        return new ResponseData<ProgressKanBanListResponse>().success(progressKanBanResponsePaginationData);
    }

    @Override
    public ResponseData<PaginationData<ProgressResponse>> getProgressPage(Long offset, Long pageSize, Long hrOrganizationId, Integer projectId, String projectManager, Integer mainCourtyard, Integer status, String projectCategory) {
        Page<Progress> progressPage = progressQueryService.getProgressPage(offset, pageSize, hrOrganizationId, projectId, projectManager, mainCourtyard, status, projectCategory);
        PaginationData<ProgressResponse> progressResponsePaginationData = adapterConverter.progressPageToPaginationData(progressPage);
        return new ResponseData<ProgressResponse>().success(progressResponsePaginationData);
    }

    @Override
    public ResponseData<PaginationData<ProgressResponse>> getProgressPageByApp(Long offset, Long pageSize, Integer projectId, Long hrOrganizationId, String appSearch, Integer progressStatus) {
        Page<Progress> progressPage = progressQueryService.getProgressPageByApp(offset, pageSize, hrOrganizationId,projectId, appSearch, progressStatus);
        PaginationData<ProgressResponse> progressResponsePaginationData = adapterConverter.progressPageToPaginationData(progressPage);
        return new ResponseData<ProgressResponse>().success(progressResponsePaginationData);
    }

    @Override
    public ResponseData<ProgressScheduleDetailsResponse> scheduleDetailsByApp(ProgressDetailRequest progressDetailRequest) {
        ProgressDetailCommand progressDetailCommand = adapterConverter.progressDetailRequestToprogressDetailCommand(progressDetailRequest);
        List<ProjectScheduleDetail> list = progressViewQueryService.scheduleDetailsByApp(progressDetailCommand);
        List<ScheduleDetailsResponse> versionDetailResponse = new ArrayList<>();
        ProgressScheduleDetailsResponse progressScheduleDetailsResponse = new ProgressScheduleDetailsResponse();
        if (list.size() > 0) {
            versionDetailResponse = adapterConverter.progressDetailToScheduleDetailsResponse(list);
            progressScheduleDetailsResponse.setScheduleDetailsResponse(versionDetailResponse);
        }

        return new ResponseData<ProgressScheduleDetailsResponse>().success(progressScheduleDetailsResponse);
    }

    @Override
    public ResponseData<PaginationData<MilestoneResponse>> getMilestonePage(Long offset, Long pageSize, Long hrOrganizationId, Integer projectId, String projectManager, Integer mainCourtyard, Integer status, String projectCategory,String projectName) {
        Page<Progress> milestonePage = milestoneQueryService.getMilestonePage(offset, pageSize, hrOrganizationId, projectId, projectManager, mainCourtyard, status, projectCategory,projectName);
        PaginationData<MilestoneResponse> milestoneResponsePaginationData = adapterConverter.milestonePageToPaginationData(milestonePage);
        return new ResponseData<MilestoneResponse>().success(milestoneResponsePaginationData);
    }

    @Override
    public ResponseData<PaginationData<KeyTaskResponse>> getKeyTaskPage(Long offset, Long pageSize, Long hrOrganizationId, Integer projectId, String projectManager, Integer mainCourtyard, Integer status, String projectCategory,String projectName) {
        Page<Progress> keyTaskPage = keyTaskQueryService.getKeyTaskPage(offset, pageSize, hrOrganizationId, projectId, projectManager, mainCourtyard, status, projectCategory,projectName);
        PaginationData<KeyTaskResponse> keyTaskResponsePaginationData = adapterConverter.keyTaskPageToPaginationData(keyTaskPage);
        return new ResponseData<KeyTaskResponse>().success(keyTaskResponsePaginationData);
    }

    @Override
    public ResponseData<PaginationData<VersionManageResponse>> getVersionList(VersionManageRequest versionManageRequest) {
       VersionManageListQuery versionManageListQuery = adapterConverter.versionManageRequestToVersionManageQuery(versionManageRequest);
        Page<VersionManage> versionList = versionManageQueryService.getVersionList(versionManageListQuery);
        PaginationData<VersionManageResponse> versionManageResponses = adapterConverter.versionManageToVersionManageResponse(versionList);
        return new ResponseData<VersionManageResponse>().success(versionManageResponses);
    }

    @Override
    public ResponseData<String> versionImport(MultipartFile projectFile, Long projectId, String scheduleName, String compileDate, String compilePerson, String description) {
        VersionManageCreateRequest versionManageCreateRequest = new VersionManageCreateRequest();
        versionManageCreateRequest.setCompileDate(compileDate);
        versionManageCreateRequest.setCompilePerson(compilePerson);
        versionManageCreateRequest.setProjectId(projectId);
        versionManageCreateRequest.setDescription(description);
        versionManageCreateRequest.setScheduleName(scheduleName);
        progressVersionDomainServer.versionImport(projectFile, versionManageCreateRequest);
        return new ResponseData<VersionManageResponse>().success("导入成功");
    }

    @Override
    public ResponseEntity<byte[]> downloadProjectTemplateFile() {
        return progressVersionDomainServer.downloadProjectTemplateFile();
    }

    @Override
    public ResponseData<List<MilestoneDetailResponse>> getMilestoneDetailList(Long projectId) {
        List<MilestoneProgressDetail> milestoneDetailList = milestoneQueryService.getMilestoneList(projectId);
        List<MilestoneDetailResponse> milestoneDetailResponses = adapterConverter.progressDetailListToMilestoneDetailListResponse(milestoneDetailList);
        return new ResponseData<MilestoneDetailResponse>().success(milestoneDetailResponses);
    }

    @Override
    public ResponseData<ProgressKanBanStatisticResponse> getProgressKanBanStatistics(Long hrOrganizationId) {
        ProgressKanBanStatisticResponse progressKanBanStatisticResponse = progressQueryService.getProgressKanStatisticBanPage(hrOrganizationId);
        return new ResponseData<ProgressKanBanStatisticResponse>().success(progressKanBanStatisticResponse);
    }

    @Override
    public ResponseData<List<ProgressKanBanLagProjectResponse>> getProgressKanBanLagProject(Long hrOrganizationId) {
        List<Progress> progressList = progressQueryService.getProgressKanLagProjectPage(hrOrganizationId);
        List<ProgressKanBanLagProjectResponse> kanBanLagProjectResponseList = adapterConverter.progressListToProgressKanBanLagProjectResponse(progressList);
        return new ResponseData<ProgressKanBanLagProjectResponse>().success(kanBanLagProjectResponseList);
    }

    @Override
    public ResponseData<VersionManageResponse> reviewPojectProgress(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        Progress progress = progressApplicationService.reviewPojectProgress(progressReviewProjectCommand);
        VersionManageResponse versionManageResponse = adapterConverter.progressToVersionManageResponse(progress);
        return new ResponseData<VersionManageResponse>().success(versionManageResponse);
    }

    @Override
    public ResponseData<VersionManageResponse> reviewPojectProgressInstitution(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        Progress progress = progressApplicationService.reviewPojectProgressInstitution(progressReviewProjectCommand);
        VersionManageResponse versionManageResponse = adapterConverter.progressToVersionManageResponse(progress);
        return new ResponseData<VersionManageResponse>().success(versionManageResponse);
    }

    @Override
    public ResponseData<ProgressViewResponse> scheduleViewAndApproval(ProgressViewRequest progressViewRequest) {
        ProgressViewCommand progressViewCommand = adapterConverter.progressViewRequestToProgressViewCommand(progressViewRequest);
        ProgressView progressView = progressViewQueryService.scheduleViewAndApproval(progressViewCommand);
        List<VersionDetailResponse> versionDetailResponse = adapterConverter.versionDetailsToVersionDetailResponse(progressView.getVersionDetail());
        ProgressViewResponse progressViewResponse = adapterConverter.progressViewToProgressViewResponse(progressView);
        progressViewResponse.setVersionDetailResponse(versionDetailResponse);
        return new ResponseData<ProgressViewResponse>().success(progressViewResponse);
    }

    @Override
    public ResponseData<Boolean> removeProgressPojectVersion(ProgressViewRequest progressViewRequest) {
        ProgressViewCommand progressViewCommand = adapterConverter.progressViewRequestToProgressViewCommand(progressViewRequest);
        progressViewQueryService.removeProgressPojectVersion(progressViewCommand);
        return new ResponseData<ProgressViewResponse>().success(true);
    }

    @Override
    public ResponseData<ProgressScheduleDetailsResponse> scheduleDetails(ProgressDetailRequest progressDetailRequest) {
        ProgressDetailCommand progressDetailCommand = adapterConverter.progressDetailRequestToprogressDetailCommand(progressDetailRequest);
        ProgressDetail progressDetail = progressViewQueryService.scheduleDetails(progressDetailCommand);
        List<ScheduleDetailsResponse> versionDetailResponse = new ArrayList<>();
        ProgressScheduleDetailsResponse progressScheduleDetailsResponse = null;
        if (progressDetail != null) {
            versionDetailResponse = adapterConverter.progressDetailToScheduleDetailsResponse(progressDetail.getProjectScheduleDetail());
            progressScheduleDetailsResponse = adapterConverter.progressDetailToProgressScheduleDetailsResponse(progressDetail);
            progressScheduleDetailsResponse.setScheduleDetailsResponse(versionDetailResponse);
        }

        return new ResponseData<ProgressScheduleDetailsResponse>().success(progressScheduleDetailsResponse);
    }

    @Override
    public ResponseData<ProgressScheduleDetailsResponse> keywordDetails(ProgressDetailRequest progressDetailRequest) {
        ProgressDetailCommand progressDetailCommand = adapterConverter.progressDetailRequestToprogressDetailCommand(progressDetailRequest);
        ProgressDetail progressDetail = progressViewQueryService.keywordDetails(progressDetailCommand);
        ProgressScheduleDetailsResponse progressScheduleDetailsResponse = null;
        List<ScheduleDetailsResponse> versionDetailResponse = new ArrayList<>();
        if (progressDetail != null) {
            versionDetailResponse = adapterConverter.progressDetailToScheduleDetailsResponse(progressDetail.getProjectScheduleDetail());
            progressScheduleDetailsResponse = adapterConverter.progressDetailToProgressScheduleDetailsResponse(progressDetail);
            progressScheduleDetailsResponse.setScheduleDetailsResponse(versionDetailResponse);
        }

        return new ResponseData<ProgressScheduleDetailsResponse>().success(progressScheduleDetailsResponse);
    }

    @Override
    public ResponseData<ProgressKanbanDetailsResponse> progressKanbanDDetails(ProgressDetailRequest progressDetailRequest) {
        ProgressDetailCommand progressDetailCommand = adapterConverter.progressDetailRequestToprogressDetailCommand(progressDetailRequest);
        ProgressKanbanDetails progressKanbanDetails = progressViewQueryService.progressKanbanDDetails(progressDetailCommand);
        ProgressKanbanDetailsResponse progressKanbanDetailsResponse = null;
        List<KanbanDetailsResponse> kanbanDetailsResponse = new ArrayList<>();
        List<MilestoneNodeResponse> milestoneNodeResponse = new ArrayList<>();
        if (progressKanbanDetails != null) {
            kanbanDetailsResponse = adapterConverter.progressKanbanDetailsToKanbanDetailsResponse(progressKanbanDetails.getKanbanDetails());
            milestoneNodeResponse = adapterConverter.progressKanbanDetailsToMilestoneNodeResponse(progressKanbanDetails.getMilestoneNodeDetails());
            progressKanbanDetailsResponse = adapterConverter.progressKanbanDetailsToProgressKanbanDetailsResponse(progressKanbanDetails);
            progressKanbanDetailsResponse.setKanbanDetailsResponse(kanbanDetailsResponse);
            progressKanbanDetailsResponse.setMilestoneNodeResponse(milestoneNodeResponse);
        }

        return new ResponseData<ProgressKanbanDetailsResponse>().success(progressKanbanDetailsResponse);
    }

    @Override
    public ResponseData<PaginationData<ProgressReportListResponse>> progressWeekReportList(ProgressReportListRequest progressReportListRequest) {
        ProgressReportListQuery progressReportListQuery = adapterConverter.progressReportListRequestToProgressReportListQuery(progressReportListRequest);
        Page<ProgressReport> progressWeekReportList = progressReportQueryService.getProgressWeekReportList(progressReportListQuery);
        PaginationData<ProgressReportListResponse> progressReportListResponse = adapterConverter.progressWeekReportListToProgressReportListResponse(progressWeekReportList);
        List<ProgressReportListResponse> records = progressReportListResponse.getRecords();
        records.forEach(v->{
            List<AttachmentEntity> entities = attachmentRepository.getAttach("progress_week_month_report", v.getId());
            v.setAttachment(adapterConverter.attachmentResp(entities));
        });
        return new ResponseData<ProgressReportListResponse>().success(progressReportListResponse);
    }

    @Override
    public ResponseData<PaginationData<ProgressReportListResponse>> progressWeekMonthReportListApp(ProgressReportListRequest progressReportListRequest) {
        ProgressReportListQuery progressReportListQuery = adapterConverter.progressReportListRequestToProgressReportListQuery(progressReportListRequest);
        Page<ProgressReport> progressWeekReportList = progressReportQueryService.getProgressWeekMonthReportListApp(progressReportListQuery);
        PaginationData<ProgressReportListResponse> progressReportListResponse = adapterConverter.progressWeekReportListToProgressReportListResponse(progressWeekReportList);
        return new ResponseData<ProgressReportListResponse>().success(progressReportListResponse);
    }

    @Override
    public ResponseData<PaginationData<ProgressReportListResponse>> progressMonthReportList(ProgressReportListRequest progressReportListRequest) {
        ProgressReportListQuery progressReportListQuery = adapterConverter.progressReportListRequestToProgressReportListQuery(progressReportListRequest);
        Page<ProgressReport> progressMonthReportList = progressReportQueryService.getProgressMonthReportList(progressReportListQuery);
        PaginationData<ProgressReportListResponse> progressReportListResponse = adapterConverter.progressWeekReportListToProgressReportListResponse(progressMonthReportList);
        List<ProgressReportListResponse> records = progressReportListResponse.getRecords();
        records.forEach(v->{
            List<AttachmentEntity> entities = attachmentRepository.getAttach("progress_week_month_report", v.getId());
            v.setAttachment(adapterConverter.attachmentResp(entities));
        });
        return new ResponseData<ProgressReportListResponse>().success(progressReportListResponse);
    }

    @Override
    public ResponseData<ProgressReportMonthDetailAllResponse> progressMonthReportDetail(ProgressReportDetailRequest progressReportDetailRequest) {
        ProgressReportExportQuery progressReportDetailQuery = adapterConverter.progressReportDetailRequestToProgressReportDetailQuery(progressReportDetailRequest);
        ProgressReportMonthDetailAllResponse progressReportMonthDetailAllResponse = progressReportQueryService.getProgressMonthReportDetail(progressReportDetailQuery);
        // 查询附件
        List<AttachmentEntity> entities = attachmentRepository.getAttach("progress_week_month_report", progressReportDetailRequest.getId());
        progressReportMonthDetailAllResponse.setAttachment(adapterConverter.attachmentResp(entities));
        return new ResponseData<ProgressReportMonthDetailAllResponse>().success(progressReportMonthDetailAllResponse);
    }

    @Override
    public ResponseData<ProgressReportWeekDetailAllResponse> progressWeekReportDetail(ProgressReportDetailRequest progressReportDetailRequest) {
        ProgressReportExportQuery progressReportExportQuery = adapterConverter.progressReportDetailRequestToProgressReportDetailQuery(progressReportDetailRequest);
        ProgressReportWeekDetailAllResponse progressReportWeekDetailAllResponse = progressReportQueryService.getProgressWeekReportDetail(progressReportExportQuery);
        List<AttachmentEntity> entities = attachmentRepository.getAttach("progress_week_month_report", progressReportDetailRequest.getId());
        progressReportWeekDetailAllResponse.setAttachment(adapterConverter.attachmentResp(entities));
        return new ResponseData<ProgressReportWeekDetailAllResponse>().success(progressReportWeekDetailAllResponse);
    }

    @Override
    public ResponseData<WeekMonthReportResponse> reviewWeekReportInstitution(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        ProgressReport weekReport = progressReportApplicationService.reviewWeekReportInstitution(progressReviewProjectCommand);
        WeekMonthReportResponse weekMonthReportResponse = adapterConverter.weekReportToWeekReportResponse(weekReport);
        return new ResponseData<WeekMonthReportResponse>().success(weekMonthReportResponse);
    }

    @Override
    public ResponseData<WeekMonthReportResponse> reviewMonthReportInstitution(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        ProgressReport weekReport = progressReportApplicationService.reviewMonthReportInstitution(progressReviewProjectCommand);
        WeekMonthReportResponse weekMonthReportResponse = adapterConverter.weekReportToWeekReportResponse(weekReport);
        return new ResponseData<WeekMonthReportResponse>().success(weekMonthReportResponse);
    }

    @Override
    public ResponseData<WeekMonthReportEidtResponse> monthWeekReportEidtDetail(ProgressWeekMonthReportRequest progressWeekMonthReportRequest) {
        ProgressWeekMonthReportCommand progressWeekMonthReportCommand = adapterConverter.progressWeekMonthReportRequestToProgressWeekMonthReportCommand(progressWeekMonthReportRequest);
        WeekMonthReportEidt weekMonthReportEidt = progressReportQueryService.monthWeekReportEidtDetail(progressWeekMonthReportCommand);
        WeekMonthReportEidtResponse weekMonthReportEidtResponse = adapterConverter.weekMonthReportEidtToWeekMonthReportEidtResponse(weekMonthReportEidt);
        List<OneAndKeyAndMilestoneTaskListResponse> oneTaskListResponse = new ArrayList<>();
        List<OneAndKeyAndMilestoneTaskListResponse> keyTaskListResponse = new ArrayList<>();
        List<OneAndKeyAndMilestoneTaskListResponse> milestoneTaskListResponse = new ArrayList<>();
        if (weekMonthReportEidt != null) {
            oneTaskListResponse = adapterConverter.weekMonthReportEidtToOneAndKeyAndMilestoneTaskListResponse(weekMonthReportEidt.getOneTaskList());
            keyTaskListResponse = adapterConverter.weekMonthReportEidtToOneAndKeyAndMilestoneTaskListResponse(weekMonthReportEidt.getKeyTaskList());
            milestoneTaskListResponse = adapterConverter.weekMonthReportEidtToOneAndKeyAndMilestoneTaskListResponse(weekMonthReportEidt.getMilestoneList());
            weekMonthReportEidtResponse.setOneTaskListResponse(oneTaskListResponse);
            weekMonthReportEidtResponse.setKeyTaskListResponse(keyTaskListResponse);
            weekMonthReportEidtResponse.setMilestoneListResponse(milestoneTaskListResponse);
        }

        // 设置附件
        List<AttachmentEntity> entities = attachmentRepository.getAttach("progress_week_month_report", progressWeekMonthReportRequest.getId());
        List<AttachmentResponse> attachmentResponses = adapterConverter.attachmentResp(entities);
        weekMonthReportEidtResponse.setAttachment(attachmentResponses);
        return new ResponseData<WeekMonthReportEidtResponse>().success(weekMonthReportEidtResponse);
    }


    @Autowired
    private AttachmentRepository attachmentRepository;
    @Override
    public ResponseData<String> monthWeekReportSaveDraftAndRelease(WeekMonthReportEidtSaveRequest weekMonthReportEidtSaveRequest) {
        WeekMonthReportEidt weekMonthReportEidt = adapterConverter.weekMonthReportEidtSaveRequestToWeekMonthReportEidt(weekMonthReportEidtSaveRequest);
        List<OneAndKeyAndMilestoneTaskList> oneTaskList = new ArrayList<>();
        List<OneAndKeyAndMilestoneTaskList> milestoneList = new ArrayList<>();
        List<OneAndKeyAndMilestoneTaskList> keyTaskList = new ArrayList<>();
        String flag = "";
        if (weekMonthReportEidt != null) {
            if (ObjectUtils.isNotNull(weekMonthReportEidtSaveRequest.getOneTaskListResponse())){
                for (OneAndKeyAndMilestoneTaskListEidtSaveRequest oneTasklists  : weekMonthReportEidtSaveRequest.getOneTaskListResponse() ){
                    oneTaskList.add(appConverter.OneAndKeyAndMilestoneTaskListEidtSaveRequestToOneAndKeyAndMilestoneTaskList(oneTasklists));
                }
            }
            if (ObjectUtils.isNotNull(weekMonthReportEidtSaveRequest.getMilestoneListResponse())){
                for (OneAndKeyAndMilestoneTaskListEidtSaveRequest milestoneLists  : weekMonthReportEidtSaveRequest.getMilestoneListResponse() ){
                    milestoneList.add(appConverter.OneAndKeyAndMilestoneTaskListEidtSaveRequestToOneAndKeyAndMilestoneTaskList(milestoneLists));
                }
            }
            if (ObjectUtils.isNotNull(weekMonthReportEidtSaveRequest.getKeyTaskListResponse())){
                for (OneAndKeyAndMilestoneTaskListEidtSaveRequest oneTasklists  : weekMonthReportEidtSaveRequest.getKeyTaskListResponse() ){
                    keyTaskList.add(appConverter.OneAndKeyAndMilestoneTaskListEidtSaveRequestToOneAndKeyAndMilestoneTaskList(oneTasklists));
                }
            }
            weekMonthReportEidt.setOneTaskList(oneTaskList);
            weekMonthReportEidt.setMilestoneList(milestoneList);
            weekMonthReportEidt.setKeyTaskList(keyTaskList);
            flag = progressReportApplicationService.monthWeekReportSaveDraftAndRelease(weekMonthReportEidt);

            // 处理附件
            List<AttachmentRequest> attachment = weekMonthReportEidtSaveRequest.getAttachment();
            attachmentRepository.save("progress_week_month_report",weekMonthReportEidtSaveRequest.getId(),attachment);
        }
        return new ResponseData<ProgressViewResponse>().success(flag);
    }


    @Override
    @SkipSqlPermission
    @SkipAuthority
    public ResponseEntity<byte[]> progressMonthWeekReportBatchExport(ProgressReportExportRequest progressReportExportRequest) throws IOException {
        ProgressReportExportQuery progressReportExportQuery = adapterConverter.progressReportExportRequestToProgressReportExportQuery(progressReportExportRequest);
        return progressReportApplicationService.progressMonthWeekReportBatchExport(progressReportExportQuery);
    }

    @Override
    @SkipSqlPermission
    @SkipAuthority
    public ResponseEntity<byte[]> projectBriefReportExport(ProjectBriefReportExportRequest projectBriefReportExportRequest) throws IOException {
        ProjectBriefReportDetailQuery projectBriefReportDetailQuery = adapterConverter.projectBriefReportExportRequestToProjectBriefReportDetailQuery(projectBriefReportExportRequest);
        return projectBriefReportApplicationService.projectBriefReportExport(projectBriefReportDetailQuery);
    }

    @Override
    public ResponseData<String> monthWeekReportCreateSum(WeekMonthReportTypeRequest weekMonthReportEidtSaveRequest) {
        WeekMonthReportType weekMonthReportType = adapterConverter.weekMonthReportEidtSaveRequestToWeekMonthReportType(weekMonthReportEidtSaveRequest);
        String date = progressReportQueryService.monthWeekReportCreateSum(weekMonthReportType);
        return new ResponseData<ProgressViewResponse>().success(date);
    }

    @Override
    public ResponseData<String> monthReportCreate(MonthReportCreateRequest monthReportCreateRequest) {
        MonthReportCreate monthReportCreate = adapterConverter.monthReportCreateRequestToMonthReportCreate(monthReportCreateRequest);
        String flag = progressReportQueryService.monthReportCreate(monthReportCreate);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> weekReportCreate(String date) {
        Boolean flag = progressReportQueryService.weekReportCreate(date);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> weekReportNews() {
        Boolean flag = progressReportApplicationService.weekReportNews();
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> monthReportNews() {
        Boolean flag = progressReportApplicationService.monthReportNews();
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> monthReportCreateTask() {
        Boolean flag = progressReportQueryService.monthReportCreateTask();
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<PaginationData<ProjectBriefListResponse>> projectBriefList(ProjectBriefListRequest projectBriefListRequest) {
        ProjectBriefListQuery projectBriefListQuery = adapterConverter.projectBriefListRequestToProjectBriefListQuery(projectBriefListRequest);
        Page<ProjectBriefReport> projectBriefReportList = projectBriefReportQueryService.getProjectBriefList(projectBriefListQuery);
        PaginationData<ProjectBriefListResponse> projectBriefListResponses = adapterConverter.projectBriefReportListToProjectBriefListResponseList(projectBriefReportList);
        return new ResponseData<ProjectBriefListResponse>().success(projectBriefListResponses);
    }

    @Override
    public ResponseData<ProjectBriefReportDetailAllResponse> projectBriefReportDetail(ProjectBriefReportDetailRequest projectBriefReportDetailRequest) {
        ProjectBriefReportDetailQuery projectBriefReportDetailQuery = adapterConverter.projectBriefReportDetailRequestToProjectBriefReportDetailQuery(projectBriefReportDetailRequest);
        ProjectBriefReportVo projectBriefReportDetail = projectBriefReportQueryService.getProjectBriefReportDetail(projectBriefReportDetailQuery);
        ProjectBriefReportDetailAllResponse projectBriefReportDetailResponses = adapterConverter.projectBriefReportDetailsToProjectBriefReportDetailResponses(projectBriefReportDetail);
        return new ResponseData<ProjectBriefReportDetailAllResponse>().success(projectBriefReportDetailResponses);
    }

    @Override
    public ResponseData<Boolean> projectBriefReportEdit(ProjectBriefReportAllRequest projectBriefReportEditRequest) {
      ProjectBriefReportEditAllCommand projectBriefReportEditCommand = adapterConverter.projectBriefReportEditRequestToProjectBriefReportEditCommand(projectBriefReportEditRequest);
        Boolean flag = projectBriefReportApplicationService.projectBriefReportEdit(projectBriefReportEditCommand);
        return new ResponseData<Boolean>().success(flag);
    }

    @Override
    public ResponseData<Boolean> projectBriefReportCreateTask() {
       Boolean flag = projectBriefReportQueryService.projectBriefCreateTask();
        return new ResponseData<Boolean>().success(flag);
    }

    @Override
    public ResponseData<ProjectResponse> projectInformation(ProgressViewRequest progressViewRequest) {
        ProgressViewCommand progressViewCommand = adapterConverter.progressViewRequestToProgressViewCommand(progressViewRequest);
        ProjectOne projectOne = progressReportApplicationService.projectInformation(progressViewCommand);
        ProjectResponse projectResponse = adapterConverter.projectOneToProjectResponse(projectOne);
        return new ResponseData<ProgressViewResponse>().success(projectResponse);
    }

    @Override
    public ResponseData<List<ProgressProcessResponse>> progressScheduleProcess(Long id) {
       List<ProcessTask> processTasks =  progressQueryService.getProgressScheduleProcess(id);
        List<ProgressProcessResponse> progressProcessResponses =  adapterConverter.processTasksToProgressProcessResponse(processTasks);
        return new ResponseData<ProgressProcessResponse>().success(progressProcessResponses);
    }

    @Override
    public ResponseData<PaginationData<ConstructionLogListResponse>> constructionLogPCList(ConstructionLogListRequest constructionLogListRequest) {
        ConstructionLogListQuery constructionLogListQuery = adapterConverter.constructionLogListRequestToConstructionLogListQuery(constructionLogListRequest);
        Page<ConstructionLog> constructionLogList = constructionLogQueryService.getConstructionLogList(constructionLogListQuery);
        PaginationData<ConstructionLogListResponse> constructionLogListResponse  = adapterConverter.constructionLogListToConstructionLogListResponse(constructionLogList);
        return new ResponseData<ConstructionLogListResponse>().success(constructionLogListResponse);
    }

    @Override
    public ResponseData<ConstructionLogListAppResponse> constructionLogAPPList(ConstructionLogListAppRequest constructionloglistapprequest) {
        ConstructionLogListQuery constructionLogListQuery = adapterConverter.constructionLogAPPListRequestToConstructionLogListQuery(constructionloglistapprequest);
        ConstructionLog constructionLogList = constructionLogApplicationService.getConstructionLogAPPList(constructionLogListQuery);
        ConstructionLogListAppResponse constructionLogListAppResponse = adapterConverter.constructionLogListToConstructionLogListAppResponse(constructionLogList);
        return new ResponseData<ConstructionLogListAppResponse>().success(constructionLogListAppResponse);
    }

    @Override
    public ResponseData<ConstructionLogDetailResponse> constructionLogDetail(ConstructionLogDetailRequest constructionlogdetailrequest) {
        ConstructionLogDetailQuery constructionLogDetailQuery = adapterConverter.constructionlogdetailrequestToConstructionLogDetailQuery(constructionlogdetailrequest);
        ConstructionLogDetail constructionLogDetail = constructionLogApplicationService.getConstructionLogDetail(constructionLogDetailQuery);
        ConstructionLogDetailResponse constructionLogDetailResponse = adapterConverter.constructionLogDetailToConstructionLogDetailResponse(constructionLogDetail);
        return new ResponseData<ConstructionLogDetailResponse>().success(constructionLogDetailResponse);
    }

    @Override
    public ResponseData<List<ConstructionLogResponse>> constructionContext(ConstructionLogRequest constructionLogRequest) {
        ConstructionLogQuery constructionLogQuery = adapterConverter.constructionLogRequestToConstructionLogQuery(constructionLogRequest);
        List<ConstructionLogWeather> constructionLogWeather = constructionLogService.constructionContext(constructionLogQuery);
        List<ConstructionLogResponse> constructionLogResponse = adapterConverter.constructionLogWeatherToConstructionLogResponse(constructionLogWeather);
        return new ResponseData<ConstructionLogResponse>().success(constructionLogResponse);
    }

    @Override
    public ResponseData<String> monthWeekConstructionSaveDraftAndRelease(ConstructionLogSaveAndDraftRequest constructionlogdetailrequest) {
        ConstructionLogSaveAndDraft constructionLogSaveAndDraft = adapterConverter.constructionlogdetailrequestToConstructionLogSaveAndDraft(constructionlogdetailrequest);
        String flag =   constructionLogApplicationService.monthWeekConstructionSaveDraftAndRelease(constructionLogSaveAndDraft);
//        String flag =  constructionLogService.monthWeekConstructionSaveDraftAndRelease(constructionLogSaveAndDraft);
        return new ResponseData<String>().success(flag);
    }

    @Override
    @SkipSqlPermission
    @SkipAuthority
    public ResponseEntity<byte[]> constructionLogExport(ConstructionLogDetailRequest constructionlogdetailrequest) throws IOException {
        ConstructionLogDetailQuery constructionLogDetailQuery = adapterConverter.constructionlogdetailrequestToConstructionLogDetailQuery(constructionlogdetailrequest);
        return constructionLogApplicationService.constructionLogExport(constructionLogDetailQuery);
    }

    @Override
    public ResponseData<Boolean> engineeringMonthReportCreateTask() {
        Boolean flag = engineeringrReportService.engineeringMonthReportCreateTask();
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<PaginationData<EngineeringReportResponse>> engineeringReportListPC(EngineeringReportListPcRequest engineeringReportListPcRequest) {
        EngineeringReportQuery engineeringReportQuery = adapterConverter.engineeringReportListPcRequestToEngineeringReportQuery(engineeringReportListPcRequest);
        Page<EngineeringWeekMonthReport> engineeringWeekMonthReport = engineeringReportApplicationService.getEngineeringReportListPC(engineeringReportQuery);
        PaginationData<EngineeringReportResponse> engineeringReportResponsePaginationData = adapterConverter.engineeringWeekMonthReportToEngineeringReportResponse(engineeringWeekMonthReport);
        return new ResponseData<EngineeringReportResponse>().success(engineeringReportResponsePaginationData);
    }

    @Override
    public ResponseData<PaginationData<EngineeringReportResponse>> engineeringReportListAPP(EngineeringReportListAppRequest engineeringReportListAppRequest) {
        EngineeringReportQuery engineeringReportQuery = adapterConverter.engineeringReportListAppRequestToEngineeringReportQuery(engineeringReportListAppRequest);
        Page<EngineeringWeekMonthReport> engineeringWeekMonthReport = engineeringReportApplicationService.getEngineeringReportListAPP(engineeringReportQuery);
        PaginationData<EngineeringReportResponse> engineeringReportResponsePaginationData = adapterConverter.engineeringWeekMonthReportToEngineeringReportResponse(engineeringWeekMonthReport);
        return new ResponseData<EngineeringReportResponse>().success(engineeringReportResponsePaginationData);
    }

    @Override
    public ResponseEntity<byte[]> downloadBySignKeys(ConstructionLogDownloadRequest constructionlogdownloadRequest) throws IOException {
        ConstructionLogDownloadQuery constructionLogDownloadQuery = adapterConverter.constructionlogdownloadRequestToConstructionLogDownloadQuery(constructionlogdownloadRequest);
        return constructionLogApplicationService.downloadBySignKeys(constructionLogDownloadQuery);
    }

    @Override
    public ResponseData<Boolean> engineeringWeekReportCreateTask() {
        Boolean flag = engineeringrReportService.engineeringWeekReportCreateTask();
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    @IdempotenceLock
    public ResponseData<Boolean> reviewEngineeringReportOrganization(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        Boolean flag = engineeringReportApplicationService.reviewEngineeringReportOrganization(progressReviewProjectCommand);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    @IdempotenceLock
    public ResponseData<Boolean> reviewEngineeringReportEPCManager(ProgressReviewProjectRequest progressReviewProjectRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.progressReviewProjectRequestToProgressReviewProjectCommand(progressReviewProjectRequest);
        Boolean flag = engineeringReportApplicationService.reviewEngineeringReportEPCManager(progressReviewProjectCommand);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseEntity<byte[]> engineeringReportExport(EngineeringReportDetailRequest engineeringReportDetailRequest) throws IOException {
        EngineeringReportDetailQuery engineeringReportDetailQuery = adapterConverter.engineeringReportDetailRequestToEngineeringReportDetailQuery(engineeringReportDetailRequest);
        return engineeringReportApplicationService.engineeringReportExport(engineeringReportDetailQuery);
    }

    @Override
    public ResponseData<EngineeringReportDetailResponse> engineeringReportDetail(EngineeringReportDetailRequest engineeringReportDetailRequest) {
        EngineeringReportDetailQuery engineeringReportDetailQuery = adapterConverter.engineeringReportDetailRequestToEngineeringReportDetailQuery(engineeringReportDetailRequest);
        EngineeringReportDetail engineeringReportDetail =  engineeringrReportService.getEngineeringReportDetail(engineeringReportDetailQuery);
        EngineeringReportDetailResponse engineeringReportDetailResponse = adapterConverter.engineeringReportDetail(engineeringReportDetail);
        return new ResponseData<EngineeringReportDetailResponse>().success(engineeringReportDetailResponse);
    }

    @Override
    public ResponseData<Boolean> engineeringReportCreateBack(EngineeringReportBackRequest engineeringReportBackRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.engineeringReportBackRequestToProgressReviewProjectCommand(engineeringReportBackRequest);
        Boolean flag = engineeringReportApplicationService.engineeringReportCreateBack(progressReviewProjectCommand);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> engineeringReportEPCBack(EngineeringReportBackRequest engineeringReportBackRequest) {
        ProgressReviewProjectCommand progressReviewProjectCommand = adapterConverter.engineeringReportBackRequestToProgressReviewProjectCommand(engineeringReportBackRequest);
        Boolean flag = engineeringReportApplicationService.engineeringReportEPCBack(progressReviewProjectCommand);
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<EngineeringReportOneQueryResponse> engineeringMonthWeekSaveDraftAndRelease(EngineeringReportListRequest engineeringReportListRequest) {
        EngineeringWeekMonthReportSave engineeringSaveAndDraft = adapterConverter.engineeringReportListRequestToEngineeringWeekMonthReportSave(engineeringReportListRequest);
        List<EngineeringLicenseFileSave> licenses = new ArrayList<>();
//        List<EngineeringImportantNodePlanSave> engineeringImportantNodePlans = new ArrayList<>();
        List<EngineeringConstructionCompletionSave> engineeringConstructionCompletions = new ArrayList<>();
        EngineeringReportOneQueryResponse engineeringReportOneQueryResponse = null;
        if (engineeringSaveAndDraft != null) {
            if (ObjectUtils.isNotNull(engineeringReportListRequest.getLicense())){
                for (EngineeringLicenseFileRepuest license  : engineeringReportListRequest.getLicense() ){
                    licenses.add(appConverter.licenseToEngineeringLicenseFileSave(license));
                }
            }
//            if (ObjectUtils.isNotNull(engineeringReportListRequest.getEngineeringImportantNodePlans())){
//                for (EngineeringImportantNodePlanRequest engineeringImportantNodePlan  : engineeringReportListRequest.getEngineeringImportantNodePlans() ){
//                    engineeringImportantNodePlans.add(appConverter.engineeringImportantNodePlanToEngineeringImportantNodePlanSave(engineeringImportantNodePlan));
//                }
//            }
            if (ObjectUtils.isNotNull(engineeringReportListRequest.getEngineeringConstructionCompletions())){
                for (EngineeringConstructionCompletionRequest engineeringConstructionCompletion  : engineeringReportListRequest.getEngineeringConstructionCompletions() ){
                    engineeringConstructionCompletions.add(appConverter.engineeringConstructionCompletionToEngineeringConstructionCompletionSave(engineeringConstructionCompletion));
                }
            }
            engineeringSaveAndDraft.setLicense(licenses);
//            engineeringSaveAndDraft.setEngineeringImportantNodePlans(engineeringImportantNodePlans);
            engineeringSaveAndDraft.setEngineeringConstructionCompletions(engineeringConstructionCompletions);
            EngineeringReportOneQuery engineeringReportOneQuery =  engineeringReportApplicationService.engineeringMonthWeekSaveDraftAndRelease(engineeringSaveAndDraft);
            engineeringReportOneQueryResponse = adapterConverter.engineeringReportOneQueryResponseToEngineeringReportOneQuery(engineeringReportOneQuery);
        }
        return new ResponseData<EngineeringReportOneQueryResponse>().success(engineeringReportOneQueryResponse);
    }

    @Override
    public ResponseData<Boolean> engineeringWeekReportNews() {
        Boolean flag = false;
        if(Tasknotice.switchIsOpen()){
             flag = engineeringReportApplicationService.engineeringWeekReportNews();
        }
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> engineeringMonthReportNews() {
        Boolean flag = false;
        if(Tasknotice.switchIsOpen()){
            flag = engineeringReportApplicationService.engineeringReportNews();
        }
        return new ResponseData<ProgressViewResponse>().success(flag);
    }

    @Override
    public ResponseData<Boolean> constructionLogDealt() {
        Boolean flag = constructionLogApplicationService.constructionLogDealt();
        return new ResponseData<Boolean>().success(flag);
    }

    @Override
    public ResponseData<Boolean> monthReportContext() {
        Boolean flag = progressReportApplicationService.monthReportContext();
        return new ResponseData<Boolean>().success(flag);
    }

    @Override
    public ResponseData<List<ScheduleDetailsResponse>> monthReportEidtDetailTaskList(ProgressWeekMonthReportRequest progressWeekMonthReportRequest) {
        ProgressWeekMonthReportCommand progressWeekMonthReportCommand = adapterConverter.progressWeekMonthReportRequestToProgressWeekMonthReportCommand(progressWeekMonthReportRequest);
        List<ProjectScheduleDetail> list = progressReportQueryService.getProjectScheduleDetailTaskList(progressWeekMonthReportCommand);
        List<ScheduleDetailsResponse> resultList = adapterConverter.progressDetailToScheduleDetailsResponse(list);
        resultList.forEach(item ->{
            List<AttachmentEntity> entities = attachmentRepository.getAttach("project_schedule_detail", item.getId());
            List<AttachmentResponse> attachmentResponses = adapterConverter.attachmentResp(entities);
            item.setAttachment(attachmentResponses);
        });
        return new ResponseData<ScheduleDetailsResponse>().success(resultList);
    }

    @Override
    public ResponseData<String> cancelVersion(ProgressViewRequest progressViewRequest) {
        ProgressViewCommand progressViewCommand = adapterConverter.progressViewRequestToProgressViewCommand(progressViewRequest);
        String result = progressVersionDomainServer.cancelVersion(progressViewCommand);
        if(result.equals("1")){
            progressReportApplicationService.sendMessageNotifyProjectManagerRole(progressViewCommand.getProjectId());
            return new ResponseData<String>().success("撤销成功");
        }else if(result.equals("0")){
            return new ResponseData<String>().failure("版本撤销","400","审批状态不为待审批，不能撤销");
        }else{
            return new ResponseData<String>().failure("版本撤销","500","程序异常");
        }

    }

    @Override
    public ResponseData<String> handleTask(Long id) {
        engineeringReportApplicationService.handleTask(id);
        return new ResponseData().success("成功");
    }


}
